package com.woniuxy.sensitive.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.sensitive.entity.SensitiveWords;
import com.woniuxy.sensitive.service.BaiduAuditService;
import com.woniuxy.sensitive.service.SensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sensitive")
public class SensitiveWordController {

    @Autowired
    private SensitiveWordService sensitiveWordService;
    @Autowired
    private BaiduAuditService baiduAuditService;

    @PostMapping("/check")
    public Map<String, Object> checkSensitiveWord(@RequestBody Map<String, String> request) {
        Map<String, Object> result = new HashMap<>();
        String text = request.get("text");
        if (text == null || text.isEmpty()) {
            result.put("code", 400);
            result.put("message", "文本不能为空");
            return result;
        }
        boolean contains = sensitiveWordService.containsSensitiveWord(text);
        result.put("code", 200);
        result.put("contains", contains);
        if (contains) {
            List<String> words = sensitiveWordService.getSensitiveWords(text);
            result.put("sensitiveWords", words);
            result.put("filteredText", sensitiveWordService.replaceSensitiveWords(text, '*'));
            // 添加百度云审核结果,返回结果有问题，注释掉了
//            result.put("baiduAuditResult", baiduAuditService.getRejectReason(text));
        }
        return result;
    }

    @PostMapping("/add")
    public Map<String, Object> addSensitiveWord(@RequestBody Map<String, String> request) {
        Map<String, Object> result = new HashMap<>();
        String word = request.get("word");
        if (word == null || word.isEmpty()) {
            result.put("code", 400);
            result.put("message", "敏感词不能为空");
            return result;
        }
        sensitiveWordService.addSensitiveWord(word);
        result.put("code", 200);
        result.put("message", "添加成功");
        return result;
    }

    @PostMapping("/remove")
    public Map<String, Object> removeSensitiveWord(@RequestBody Map<String, String> request) {
        Map<String, Object> result = new HashMap<>();
        String word = request.get("word");
        if (word == null || word.isEmpty()) {
            result.put("code", 400);
            result.put("message", "敏感词不能为空");
            return result;
        }
        sensitiveWordService.removeSensitiveWord(word);
        result.put("code", 200);
        result.put("message", "移除成功");
        return result;
    }
    @PutMapping("/update")
    public Map<String, Object> updateSensitiveWord(@RequestBody Map<String, String> request) {
        Map<String, Object> result = new HashMap<>();
        String oldWord = request.get("oldWord");
        String newWord = request.get("newWord");
        if (oldWord == null || newWord == null || oldWord.isEmpty() || newWord.isEmpty()) {
            result.put("code", 400);
            result.put("message", "参数不能为空");
            return result;
        }
        sensitiveWordService.removeSensitiveWord(oldWord);
        sensitiveWordService.addSensitiveWord(newWord);
        result.put("code", 200);
        result.put("message", "更新成功");
        return result;
    }
    @GetMapping("/list")
    public Map<String, Object> listSensitiveWords(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String word) {
        Map<String, Object> result = new HashMap<>();
        try {
            Page<SensitiveWords> page = sensitiveWordService.listSensitiveWords(pageNum, pageSize, word);
            result.put("code", 200);
            result.put("data", page);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "服务器错误");
        }
        return result;
    }


}
